package com.df.demo.apicloud;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;

import com.uzmap.pkg.openapi.ExternalActivity;
import com.uzmap.pkg.openapi.Html5EventListener;
import com.uzmap.pkg.openapi.WebViewProvider;
import com.uzmap.pkg.uzcore.uzmodule.UZModuleContext;

import org.json.JSONObject;

/**
 * ApiCloude官网编译的supperWebView so库有问题，需要删除
 * armeabi-v7a文件夹才能真机运行
 */
public class MainActivity extends ExternalActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "==onCreate");
//        setContentView(R.layout.activity_main);
        bindSomething();
    }

    private void bindSomething(){
        //设置一个监听，监听来自Html5页面发送出来的事件
        //此处我们监听"abc"事件，监听Html5页面通过api.sendEvent发出"abc"事件时
        addHtml5EventListener(new Html5EventListener("abc-sendEvent") {
            @Override
            public void onReceive(WebViewProvider provider, Object extra) {
                showAlert("收到来自Html5的abc-sendEvent事件，传入的参数为：\n\n" + extra + "\n\n");
            }
        });
    }

    /**
     * 重写该函数，可实现处理收到来自Html5页面的操作请求，处理完毕后异步回调至Html5
     */
    @Override
    protected boolean onHtml5AccessRequest(WebViewProvider provider, UZModuleContext moduleContext) {
        String name = moduleContext.optString("name");
        Log.d(TAG, "===收到accessNative请求：" + name);
        //"requestEvent"与assets/widget/html/wind.html页面的发送请求相匹配
        if("requestEvent".equals(name)){
            JSONObject extra = new JSONObject();
            try{
                extra.put("value", "哈哈哈，我是来自Native的事件");
            }catch(Exception e){
                e.printStackTrace();
            }
            //"fromNative"与assets/widget/html/wind.html页面的apiready中注册的监听相对应
            sendEventToHtml5("fromNative", extra);
            return true;
        }else if("abc-native".equals(name)){
            defaultHandleHtml5AccessRequest(moduleContext);
        }
        return true;
    }

    //默认处理收到收到来自Html5页面的操作请求，并通过UZModuleContext给予JS回调
    private void defaultHandleHtml5AccessRequest(final UZModuleContext moduleContext){
        String name = moduleContext.optString("name");
        Object extra = moduleContext.optObject("extra");
        AlertDialog.Builder dia = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT);
        dia.setTitle("提示消息");
        dia.setMessage("收到来自Html5页面的操作请求，访问的名称标识为：\n[" + name + "]\n传入的参数为：\n[" + extra + "]\n\n" + "是否处理？\n");
        dia.setCancelable(false);
        dia.setPositiveButton("不处理", null);
        dia.setNegativeButton("处理", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                JSONObject json = new JSONObject();
                try{
                    json.put("result0", "value0");
                    json.put("result1", "value1");
                    json.put("result2", "value2");
                }catch(Exception e){
                    ;
                }
                moduleContext.success(json, true);
            }
        });
        dia.show();
    }

    private void showAlert(String message){
        AlertDialog.Builder dia = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT);
        dia.setTitle("提示消息");
        dia.setMessage(message);
        dia.setCancelable(false);
        dia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dia.show();
    }

    @Override
    protected void onPageStarted(WebViewProvider provider, String url, Bitmap favicon) {
        super.onPageStarted(provider, url, favicon);
        Log.d(TAG, "==onPageStarted");
    }

    @Override
    protected void onReceivedTitle(WebViewProvider provider, String title) {
        super.onReceivedTitle(provider, title);
        Log.d(TAG, "==onReceivedTitle");
    }

    @Override
    protected void onPageFinished(WebViewProvider provider, String url) {
        super.onPageFinished(provider, url);
        Log.d(TAG, "==onPageFinished");
    }

    /**
     * 重写该函数，可实现处理当某Webview即将加载某Url时，是否进行拦截，拦截后，该Webview将不继续加载该Url
     */
    @Override
    protected boolean shouldOverrideUrlLoading(WebViewProvider provider, String url) {
        Log.d(TAG, "==shouldOverrideUrlLoading");
        if(url.contains("taobao")){
            showAlert("不允许访问淘宝！");
            return true;
        }
        return false;
    }

    @Override
    protected void onProgressChanged(WebViewProvider provider, int newProgress) {
        super.onProgressChanged(provider, newProgress);
        Log.d(TAG, "==onProgressChanged");
    }
}
